<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      
      <el-form-item label="请求情况" prop="isDone">

        <el-select v-model="queryParams.isDone">
            <el-option value="w" label="等待处理"/>
            <el-option value="g" label="正在处理"/>
            <el-option value="n" label="无法处理"/>
            <el-option value="y" label="已处理"/>
            <el-option value="q" label="已取消"/>
          </el-select>

      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="serviceRequestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入住id" align="center" prop="checkinId" />
      <el-table-column label="服务内容" align="center" prop="serviceRequestContent" />
      <el-table-column label="服务时间" align="center" prop="serviceRequestTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serviceRequestTime, '{y}-{m}-{d} {hh}:{ii}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="请求情况" align="center" prop="isDone" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务情况" prop="isDone">
          <el-select v-model="form.isDone">

            <el-option value="w" label="等待处理"/>
            <el-option value="g" label="正在处理"/>
            <el-option value="n" label="无法处理"/>
            <el-option value="y" label="已处理"/>
            <el-option value="q" label="已取消"/>

          </el-select>
        </el-form-item>
        <el-form-item label="事件" prop="event">

            <el-input v-model="form.event" placeholder="请填写详情" />

        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServiceRequest, getServiceRequest, delServiceRequest, addServiceRequest, updateServiceRequest } from "@/api/hotel/serviceRequest";

export default {
  name: "ServiceRequest",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      serviceRequestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkinId: undefined,
        serviceRequestContent: undefined,
        serviceRequestTime: undefined,
        isDone: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        serviceRequestId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        checkinId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        serviceRequestContent: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        serviceRequestTime: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        isDone: [
          { required: true, message: "w=等待处理，n=无法处理，y=已处理不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listServiceRequest(this.queryParams).then(response => {
        this.serviceRequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        serviceRequestId: undefined,
        checkinId: undefined,
        serviceRequestContent: undefined,
        serviceRequestTime: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        isDone: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.serviceRequestId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const serviceRequestId = row.serviceRequestId || this.ids
      getServiceRequest(serviceRequestId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "更改请求情况";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if(this.form.isDone)
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.serviceRequestId != null && this.form.isDone!="y"&&this.form.isDone!="q") {
            updateServiceRequest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            this.$modal.confirm("不能对已处理或已取消的需求操作");
            this.buttonLoading = false;
          }
        }
      });
    },
    statusFormat(row,column){
      if(row.isDone=="w"){
        return "等待处理";
      }
      else if(row.isDone=="n"){
        return "无法处理";
      }
      else if(row.isDone=="y"){
        return "已处理";
      }
      else if(row.isDone=="q"){
        return "已取消";
      }
      else if(row.isDone=="g"){
        return "正在处理";
      }
    }
    
  }
};
</script>
